import { _decorator, Component, Node, tween, TweenAction, Vec3,easing, Vec2, random, randomRange } from 'cc';
import { ResourceManager } from '../PlayableFramework/Tools/Manager/ResourceManager';
import { Actor } from './Actor';
import { PoolObject } from '../PlayableFramework/Tools/Pool/PoolObject';
const { ccclass, property } = _decorator;

@ccclass('MoneyGen')
export class MoneyGen extends Component {

    private list:PoolObject[] = [];
    private index :number;
    protected onLoad(): void {
        ResourceManager.load(()=>{

            for (let y = 0; y < 12; y++) {
                for (let x = 0; x < 5; x++) {
                    for (let z = 0; z < 5; z++) {
                        var obj = ResourceManager.spawn("MoneyStack",this.node.children[0]);
                        obj.node.position = new Vec3(x*0.45,y*0.5,z*0.8);
                        this.list.push(obj);
                    }
                }
            }

            this.index = 50;
            for (let i = 0; i < this.list.length; i++) {
                const element = this.list[i];
                element.node.active = i<this.index;
            }
        }).add("MoneyStack").add("MoneyEffect");

    } 

    public collect(target:Node,func:() => void){
        let _list :PoolObject[]=[];
        for (let i = 0; i < this.index; i++) {
            const element = this.list[i];
            _list.push(element);
        }

        _list =  _list.sort((a,b)=>{
            var node = this.node.children[1];
            var aDis = Vec3.distance(a.node.worldPosition,node.worldPosition);
            var bDis = Vec3.distance(b.node.worldPosition,node.worldPosition);

            if (aDis < bDis) {
                return -1; // a 应排在 b 之前
            }
            if (aDis > bDis) {
                return 1; // a 应排在 b 之后
            }
            return 0; // a 和 b 相等
        })

        for (let i = 0; i < _list.length; i++) {
            const obj = _list[i];
            const originPos = obj.node.worldPosition.clone();
            var delay = 0.005* i;
            tween(obj.node).delay(delay).to(0.2,
                {worldPosition:obj.node.worldPosition.clone().add(new Vec3(0,3,0)),eulerAngles: new Vec3(randomRange(0,180),randomRange(0,180),randomRange(0,180))},
                {easing:easing.sineOut}).to(0.2,
                {worldPosition:target.worldPosition,eulerAngles: new Vec3(0,0,0)},
                {easing:easing.sineIn}).call(()=>{
                func();
                obj.node.worldPosition = originPos;
                obj.node.active = false;
            }).start();
            
        }
    }

    public gen(max:number,pos:Vec3,func:()=>void){
        this.index = max;
        for (let i = 0; i < 10; i++) {
            const index = i;
            const delay = i * 0.02;
            const obj = ResourceManager.spawn("MoneyStack",this.node);
            obj.node.worldPosition = pos;
            tween(obj.node).delay(delay).to(0.3,{worldPosition:this.node.children[1].worldPosition}).call(()=>{
                ResourceManager.despawn(obj,ResourceManager.getPoolNode("EffectMoneyPools"));
                if(index == 0){
                    for (let j = 0; j < max; j++) {
                        const element = this.list[j];
                        element.node.active = true;
                    }
                    tween(this.node).to(0.1,{worldScale:new Vec3(1,1.5,1)}).to(0.1,{worldScale:new Vec3(1,1,1)}).start();
                    func();
                }
            }).start();
        }
    }

}


